/**
 * 描述：同意词
 * 作者：彭涛涛
 * 日期：2019/6/5
 * 版本：0.0.1
 **/
(function(E, KIT, RES) {
  "use strict";

  var __URL_ROOT = RES.URL_ROOT + "/synonym";

  //region private  新增编辑操作
  var _addOrUpdate = function(e) {

      var __grid = this;

      rapid.confirm("确定要保存吗？", function(yes) {
        if(yes) {

          rapid.post({
            url: __URL_ROOT + "/saveObjects",
            data: $.extend(true, {}, e.serializeJson()),
            success: function(result) {
              e.hide();

              rapid.success("保存成功。", function() {
                __grid.reload();
              }, false);
            }
          });
        }
      });
    },
    _addOrUpdateDetail = function(e) {

      var __grid = this;

      rapid.confirm("确定要保存吗？", function(yes) {
        if(yes) {

          var key = rapid("#gridSynIndex").grid().selection()[0].ui().conflict_id.val();
          rapid.post({
            url: __URL_ROOT + "/saveDetailObjects",
            data: $.extend(true, {}, e.serializeJson(), {row_id: key}),
            success: function(result) {
              e.hide();

              rapid.success("保存成功。", function() {
                __grid.reload();
              }, false);
            }
          });
        }
      });
    };
  //endregion

  rapid.Page.create({
    render: {
      titleBar: "同意词",

      header: E.view({cssClass: "fn-section r-row r-p-sm"},
        E.textbox({id: "txtSearchAttrib_01", mode: "search", tip: "名称...", cssClass: "r-col-6"})
      ),

      main: E.grid("gridSynIndex"),

      other: E.block(
        E.window("wndSynDetail",
          E.grid("gridSynDetail")
        )
      )
    },
    methods: {
      //region private 主表
      //同意词index-grid
      _getGridFields_: function() {
        return [{
          title: "名称", name: "attrib_01", width: 600, frozen: true, editor: {
            validator: {
              rules: {required: true, maxlength: 100}
            }
          }
        }];
      },
      _initGridEvent_: function() {
        rapid("#gridSynIndex").grid()
                              .on("add", _addOrUpdate)
                              .on("update", _addOrUpdate)
                              .on("remove", function(ids) {
                                var __grid = this;
                                rapid.post({
                                  url: __URL_ROOT + "/deleteObjects",
                                  data: "ids=" + ids,
                                  success: function() {
                                    __grid.reload();
                                  }
                                });
                              });
      },
      //单选按钮
      _getGridSingeActions_: function() {
        var __app = this;
        return [{
          "同意词集合": function(e) {
            __app._openDetail_(this.selection()[0].ui().conflict_id.val(), "");
          }
        }];
      },

      _initGrid_: function() {
        var __app = this;
        rapid("#gridSynIndex").grid({
          height: function() {
            return $(__app.main()).height();
          },

          addable: true,
          updatable: true,
          removable: true,
          detailed: true,

          dataSource: {
            url: __URL_ROOT + "/getObjects"
          },
          pageable: true,
          keyFields: "conflict_id",
          paramFields: ["conflict_id"],
          fields: this._getGridFields_(),
          singleActions: this._getGridSingeActions_()
        });

        this._initGridEvent_();
      },

      _initUi_: function() {
        var __app = this;

        //查询
        rapid.ui("#txtSearchAttrib_01").on("submit", function() {
          rapid("#gridSynIndex").grid().load({
            data: {
              attrib_01: this.val()
            }
          });
        });
      },
      //endregion

      //region private 子表
      //同意词 detail-grid
      _getDetailGridFields_: function() {
        return [{
          title: "名称", name: "attrib_01", width: 500, frozen: true, editor: {
            validator: {
              rules: {required: true, maxlength: 100}
            }
          }
        }];
      },
      _initDetailGridEvent_: function() {
        rapid("#gridSynDetail").grid()
            .on("add", _addOrUpdateDetail)
            .on("update", _addOrUpdateDetail)
            .on("remove", function(ids) {
              var __grid = this;
              rapid.post({
                url: __URL_ROOT + "/deleteDetailObjects",
                data: "ids=" + ids,
                success: function() {
                  __grid.reload();
                }
              });
            });
      },
      _initDetailGrid_: function() {
        var __app = this;
        rapid("#gridSynDetail").grid({
          height: function() {
            return rapid("#wndSynDetail").window().height();
          },

          addable: true,
          updatable: true,
          removable: true,
          detailed: true,

          dataSource: {
            url: __URL_ROOT + "/getSynDetails"
          },
          pageable: true,
          keyFields: "conflict_id",
          paramFields: ["conflict_id"],
          fields: this._getDetailGridFields_()
        });

        this._initDetailGridEvent_();
      },
      _openDetail_: function(id) {
        var __grid = this;
        rapid("#wndSynDetail").window({
          title: "同意词集合",
          size: "xl",
          height: "100%",
          buttons: null,
          onInit: function() {
            __grid._initDetailGrid_();
          },
          onShow: function() {
            rapid("#gridSynDetail").grid().load({
              data: "conflict_id=" + rapid("#gridSynIndex").grid().selection()[0].ui().conflict_id.val()
            });
          },
          onHide: function() {
            rapid("#gridSynIndex").grid().reload();
          }
        }).show();
      }
      //endregion

    }
  })(function() {

    this._initUi_();

    this._initGrid_();

    rapid("#gridSynIndex").grid().load();
  }).init();

})(rapid.html.Element, rapid.kit, rapid.res);